package com.iceengine.j3d.objects;

/**
 * This class is used to draw quads.
 * @author penguin
 */
import org.lwjgl.opengl.GL11;

import org.lwjgl.util.Color;
import org.newdawn.slick.opengl.Texture;
import org.lwjgl.util.vector.Vector3f;
public class Quad implements IFigure {
    private Texture theTexture;
    public Vertex[] Vertices;
    private Color theColor;
    private Vector3f Rotation;
    private int displayList;

    /**
     * Creates the quad with given texture and color.
     * @param texture The texture to draw the object with.
     * @param color The color to draw the object with.
     */
    public Quad(Texture texture, Color color)
    {
        theTexture = texture;
        Vertices = new Vertex[4];
        Rotation = new Vector3f(0,0,0);
        theColor = color;
        displayList = GL11.glGenLists(1);
    }

    /**
     * Creates a quad with given vertices, texture and color.
     * @param p1 The first vertex.
     * @param p2 The second vertex.
     * @param p3 The third vertex.
     * @param p4 The fourth vertex.
     * @param texture The texture to draw the object with.
     * @param color The color to draw the object with.
     */
    public Quad(Vertex p1, Vertex p2, Vertex p3, Vertex p4, Texture texture, Color color) {
        Vertices = new Vertex[4];
        Vertices[0] = p1;
        Vertices[1] = p2;
        Vertices[2] = p3;
        Vertices[3] = p4;
        theTexture = texture;
        Rotation = new Vector3f(0,0,0);
        theColor = color;
        displayList = GL11.glGenLists(1);
        genObject();
    }

    public IFigure cloneMe()
    {
        return new Quad(Vertices[0],Vertices[1], Vertices[2], Vertices[3], theTexture, theColor);
    }

    public void setVertex(int id, Vertex theVertex)
    {
        Vertices[id] = theVertex;
        //genObject();
    }

    public Vertex getVertex(int id)
    {
        return Vertices[id];
    }


    public void DrawObject()
    {
        GL11.glRotatef(Rotation.x, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(Rotation.y, 0.0f, 1.0f, 0.0f);
        GL11.glRotatef(Rotation.z, 0.0f, 0.0f, 1.0f);

        if(theTexture != null)
        {
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            theTexture.bind();
        }
            
        if(theColor != null)
            GL11.glColor3f(theColor.getRed(), theColor.getGreen(), theColor.getBlue());

        GL11.glCallList(displayList);

        if(theTexture != null)
        {
            GL11.glDisable(GL11.GL_TEXTURE_2D);
        }
    }

    public void genObject()
    {
        GL11.glNewList(displayList, GL11.GL_COMPILE);
        GL11.glBegin(GL11.GL_QUADS);
            Vertices[0].drawVertex();
            Vertices[1].drawVertex();
            Vertices[2].drawVertex();
            Vertices[3].drawVertex();
        GL11.glEnd();
        GL11.glEndList();
    }

    public void setRotation(Vector3f rotation)
    {
        Rotation = rotation;
    }

}
